import sys
import os
import dlib
import glob
from skimage import io
import numpy as np
import cv2
import time
import imutils
from imutils import face_utils
from scipy.spatial import distance as dist

# 睁眼率
def eye_aspect_ratio(eye):
    A = dist.euclidean(eye[1], eye[5])
    B = dist.euclidean(eye[2], eye[4])
    C = dist.euclidean(eye[0], eye[3])
    ear = (A + B) / (2.0 * C)
    return ear


WINDOWS_WIDTH = 640
WINDOWS_HEIGHT = 480
EYE_AR_THRESH = 0.2
EYE_AR_CONSEC_FRAMES = 2
COUNTER = 0
TOTAL = 0
FPS = 0

predictor_path = 'shape_predictor_68_face_landmarks.dat'
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor(predictor_path)

(lStart, lEnd) = face_utils.FACIAL_LANDMARKS_IDXS["left_eye"]
(rStart, rEnd) = face_utils.FACIAL_LANDMARKS_IDXS["right_eye"]

cap = cv2.VideoCapture(0)
while (True):
    start_time = time.time()
    ret, frame = cap.read()
    frame = cv2.flip(frame, 1)
    img = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)
    dets = detector(img, 1)
    for index, det in enumerate(dets):
        right_top = (det.right(), det.top())
        left_bottom = (det.left(), det.bottom())
        # 画面部检测框
        cv2.rectangle(frame, right_top, left_bottom, (255, 0, 0), 2)

        # 面部特征点检测
        shape = predictor(img, det)
        landmarks = np.matrix([[p.x, p.y] for p in shape.parts()])
        for idx, point in enumerate(landmarks):
            # 68点的坐标
            pos = (point[0, 0], point[0, 1])
            cv2.circle(frame, pos, 2, (0, 255, 0), -1)

        shape = face_utils.shape_to_np(shape)
        leftEye = shape[lStart:lEnd]
        rightEye = shape[rStart:rEnd]
        leftEAR = eye_aspect_ratio(leftEye)
        rightEAR = eye_aspect_ratio(rightEye)
        ear = (leftEAR + rightEAR) / 2.0

        leftEyeHull = cv2.convexHull(leftEye)
        rightEyeHull = cv2.convexHull(rightEye)
        cv2.drawContours(frame, [leftEyeHull], -1, (0, 255, 0), 1)
        cv2.drawContours(frame, [rightEyeHull], -1, (0, 255, 0), 1)

        if ear < EYE_AR_THRESH:
            COUNTER += 1
        else:
            if COUNTER >= EYE_AR_CONSEC_FRAMES:
                TOTAL += 1
            COUNTER = 0

        cv2.putText(frame, 'EAR: {:.2f}'.format(ear), (WINDOWS_WIDTH - 120,
                                                       20 + 30), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 1, cv2.LINE_AA)
        cv2.putText(frame, 'State: {}'.format('OPEN' if ear > 0.2 else 'CLOSE'), (WINDOWS_WIDTH -
                                                                                  120, 20 + 30 + 30), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 1, cv2.LINE_AA)
        cv2.putText(frame, 'Blind: {}'.format(TOTAL), (20, 20+30),
                    cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 1, cv2.LINE_AA)

    end_time = time.time()
    FPS = 1 / (end_time - start_time)
    cv2.putText(frame, 'There are {} faces in the picture.'.format(
        len(dets)), (20, 20), cv2.FONT_HERSHEY_COMPLEX, 0.5, (0, 0, 255), 1, cv2.LINE_AA)
    cv2.putText(frame, 'FPS: {:.4f}'.format(FPS), (WINDOWS_WIDTH-120, 20), cv2.FONT_HERSHEY_COMPLEX,
                0.5, (0, 0, 255), 1, cv2.LINE_AA)

    cv2.namedWindow("Cap", 0)
    cv2.resizeWindow("Cap", WINDOWS_WIDTH, WINDOWS_HEIGHT)
    cv2.imshow('Cap', frame)
    # cv2.imshow('Gray', img)
    key = cv2.waitKey(5)
    if key == 27:
        break

cap.release()
cv2.destroyAllWindows()
